﻿using MiniFox.Utility;
using MiniFox.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;

namespace MiniFox.Net.Models
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class WebMessageRequestBase
    {
        NameValueCollection _query;
        IDictionary<string, object> _headers;
        HttpMethod _method;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="method"></param>
        protected internal WebMessageRequestBase(HttpMethod method) : this(method, null)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="method"></param>
        /// <param name="query"></param>
        protected internal WebMessageRequestBase(HttpMethod method, object query) : this(method, query, null)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="method"></param>
        /// <param name="query"></param>
        /// <param name="headers"></param>
        protected internal WebMessageRequestBase(HttpMethod method, object query, object headers)
        {
            _method = method;
            if (query != null)
            {
                _query = query.ToNameValueCollection();
            }
            if (headers != null)
            {
                _headers = headers.ToDictionary();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public HttpMethod Method
        {
            get { return this._method; }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual NameValueCollection Query
        {
            get { return _query; }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual IDictionary<string, object> Headers
        {
            get { return this._headers; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected internal virtual HttpRequestMessage CreateHttpRequest()
        {
            var request = new HttpRequestMessage();
            request.Method = this.Method;
            request.RequestUri = request.RequestUri.AddQueryString(Query);
            request.AddHttpHeaders(this.Headers);
            request.Content = this.CreateHttpContent();
            return request;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected internal abstract HttpContent CreateHttpContent();

    }
}
